/*
 *-----------------------------------------------------------------------------
 *
 * TEditorRpcDispatcher.java
 *
 * This file was created from IEditorRpc.idl
 * by the Simple JavRMI backend.
 *
 * Copyright 2008-2010, Evgeny Alexeyev
 * Licensed under the EPL - v1.0
 * http://www.eclipse.org/legal/epl-v10.html
 *
 *-----------------------------------------------------------------------------
 */

package com.googlecode.umlsynch.rpc;

import java.io.*;

import com.googlecode.umlsynch.rpc.base.TIOHandler;

import com.googlecode.umlsynch.rpc.base.IDebug;

import com.googlecode.umlsynch.rpc.base.TDebug;

import com.googlecode.umlsynch.rpc.base.IDispatcher;

import com.googlecode.umlsynch.rpc.TFileDescriptor;

import com.googlecode.umlsynch.rpc.TClassDescriptor;

import com.googlecode.umlsynch.rpc.IEditorRpc;


public class TEditorRpcDispatcher implements IDispatcher
{
  private IEditorRpc implementation = null;
  private TDebug debug;
  
  public TEditorRpcDispatcher(IDebug dbg, IDispatcher disp, IEditorRpc impl) {
    debug = new TDebug(dbg);
    if (disp != null)
      disp.register(this);
    
    implementation = impl;
  }
  public boolean match(String interfaceName) {
    if (interfaceName.equals("IEditorRpc"))
      return true;
    return false;
  }
  
  public void register(IDispatcher disp) {
    disp.register(this);
  }
  
  public void process(TIOHandler handler) {
    String data = handler.readString();
    
    debug.Note("Recieved methodname:" + data);
    
    
    if (data.equals( "OpenMethodImplementation")) {
      OpenMethodImplementation(handler);
      return;
    }
    
    if (data.equals( "OpenMethodDeclaration")) {
      OpenMethodDeclaration(handler);
      return;
    }
    
    if (data.equals( "OpenClass")) {
      OpenClass(handler);
      return;
    }
    
    if (data.equals( "OpenFile")) {
      OpenFile(handler);
      return;
    }
    
    debug.Error("Unknown method name:" + data);
  }
  
  private void OpenMethodImplementation(TIOHandler handler) {
    String data = null;
    TClassOperation desc;
    
    
    try {
      data = handler.readString();
      if (data.equals("TClassOperation")) {
        desc = (TClassOperation) handler.getObjectInputStream().readObject();
      } else {
        debug.Error("Recieved wrong parameter type:" + data + " expected TClassOperation");
        throw new ClassNotFoundException();
      }
      
      implementation.OpenMethodImplementation(desc);
    }
    catch (IOException e) {
    }
    catch (ClassNotFoundException e) {
    }
  }; // 
  
  
  private void OpenMethodDeclaration(TIOHandler handler) {
    String data = null;
    TClassOperation desc;
    
    
    try {
      data = handler.readString();
      if (data.equals("TClassOperation")) {
        desc = (TClassOperation) handler.getObjectInputStream().readObject();
      } else {
        debug.Error("Recieved wrong parameter type:" + data + " expected TClassOperation");
        throw new ClassNotFoundException();
      }
      
      implementation.OpenMethodDeclaration(desc);
    }
    catch (IOException e) {
    }
    catch (ClassNotFoundException e) {
    }
  }; // 
  
  
  private void OpenClass(TIOHandler handler) {
    String data = null;
    TClassDescriptor desc;
    
    
    try {
      data = handler.readString();
      if (data.equals("TClassDescriptor")) {
        desc = (TClassDescriptor) handler.getObjectInputStream().readObject();
      } else {
        debug.Error("Recieved wrong parameter type:" + data + " expected TClassDescriptor");
        throw new ClassNotFoundException();
      }
      
      implementation.OpenClass(desc);
    }
    catch (IOException e) {
    }
    catch (ClassNotFoundException e) {
    }
  }; // 
  
  
  private void OpenFile(TIOHandler handler) {
    String data = null;
    TFileDescriptor desc;
    Integer line;
    
    
    try {
      data = handler.readString();
      if (data.equals("TFileDescriptor")) {
        desc = (TFileDescriptor) handler.getObjectInputStream().readObject();
      } else {
        debug.Error("Recieved wrong parameter type:" + data + " expected TFileDescriptor");
        throw new ClassNotFoundException();
      }
      
      data = handler.readString();
      if (data.equals("Integer")) {
        line = (Integer) handler.getObjectInputStream().readObject();
      } else {
        debug.Error("Recieved wrong parameter type:" + data + " expected Integer");
        throw new ClassNotFoundException();
      }
      
      implementation.OpenFile(desc, line);
    }
    catch (IOException e) {
    }
    catch (ClassNotFoundException e) {
    }
  }; // 
  
}; // IEditorRpc

